package com.jt.datasource;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisDataSource {
    private static volatile JedisPool jedisPool;
    //解决 禁止指令重排序
    //保证线程可见性
//懒加载
    public static Jedis getConnection(){
        if (jedisPool==null) {
            synchronized (JedisDataSource.class) {
                if (jedisPool == null) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(128);//最大连接数
                    poolConfig.setMaxIdle(8);//最大空闲连接数
                    jedisPool = new JedisPool(poolConfig, "192.168.126.129", 6379);
                }
            }
        }
        return jedisPool.getResource();
    }

    public static void close(){
        jedisPool.close();
    }
}
